﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using OnlineTestBLL;

namespace OnlineTestUI.BackManager
{
    public partial class questionlist : System.Web.UI.Page
    {
        private QuestionsBLL qb = new QuestionsBLL();
        private int? _id;
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!string.IsNullOrEmpty(Request.QueryString["typeid"]))
            {
                _id = Convert.ToInt32(Request.QueryString["typeid"]);
            }
            if (!IsPostBack)
            {
                BindGrid(_id);
            }
            
        }

        #region BindGrid

        private void BindGrid(int? typeid)
        {
            // 1.设置总项数（特别注意：数据库分页一定要设置总记录数RecordCount）
            //选出某类型
            if (typeid != null)
            {
                GridQuestion.RecordCount = qb.GetRecordCount(" questiontypeid =" + typeid.ToString() + " and status =1 and versionnumber != 0 ");

            }
            else
            {
                GridQuestion.RecordCount = qb.GetRecordCount("  status =1 and versionnumber != 0 ");
            }

             // 2.获取当前分页数据
                DataTable table = GetPagedDataTable(typeid);

                GridQuestion.DataSource = table;

                GridQuestion.DataBind();
        }


        /// <summary>
        /// 数据库分页
        /// </summary>
        /// <returns></returns>
        private DataTable GetPagedDataTable(int? typeid)
        {
            int pageIndex = GridQuestion.PageIndex;
            int pageSize = GridQuestion.PageSize;

            string sortField = GridQuestion.SortField;
            string sortDirection = GridQuestion.SortDirection;

            int rowbegin = pageIndex * pageSize+1;
            int rowend = (pageIndex + 1) * pageSize;
            string orderby = String.Format("{0} {1}", sortField, sortDirection);
            DataTable questionList;
            if (typeid != null)
            {
                questionList =
                    qb.GetListByPage(" questiontypeid =" + typeid.ToString() + " and status =1 and versionnumber != 0 ", orderby,
                                     rowbegin, rowend)
                      .Tables[0];
            }
            else
            {
                questionList =
                   qb.GetListByPage(" status =1 and versionnumber != 0 ", orderby,
                                    rowbegin, rowend)
                     .Tables[0];
            }


            return questionList;
        }

        #endregion

        #region Events

       


        protected void GridQuestion_PageIndexChange(object sender, FineUI.GridPageEventArgs e)
        {
            GridQuestion.PageIndex = e.NewPageIndex;

            BindGrid(_id);
        }

        protected void GridQuestion_Sort(object sender, FineUI.GridSortEventArgs e)
        {
            GridQuestion.SortDirection = e.SortDirection;
            GridQuestion.SortField = e.SortField;

            BindGrid(_id);
        }

        #endregion


    }
}